﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using System.Data.SqlClient;
using System.Data.SqlTypes;
using AjaxControlToolkit;

namespace NSATBPortal
{
    public partial class RunsEdit : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            
            if (!IsPostBack)
            {
                string sOp = Request.QueryString["Op"].ToString();

                if (sOp == "Edit")
                {
                    string sName = Server.UrlDecode(Request.QueryString["Name"].ToString());
                    lblParent.Text = "Edit Run Schedule " + sName;
                }
                else
                {
                    lblParent.Text = "Add Run Schedule";
                }

                // pre-populate rundef
                using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                {
                    DataTable dtList;
                    dtList = objATBDB.GetDefRunList();
                    
                    dropdownDefRun.DataValueField = "ID";
                    dropdownDefRun.DataTextField = "Name";
                    dropdownDefRun.DataSource = dtList;
                    dropdownDefRun.DataBind();
                    dropdownDefRun.Items.Add(new ListItem("[ Select ]", "0"));
                    dropdownDefRun.SelectedValue = "0";
                }


                if (sOp == "Edit")
                {
                    int iID = Convert.ToInt32(Server.UrlDecode(Request.QueryString["ID"].ToString()));

                    using (ATBSchemaDB objATBDB = new ATBSchemaDB())
                    {
                        DataTable dtDetail;
                        dtDetail = objATBDB.GetRunsDetail(iID);

                        lblSchedule.Visible = false;
                        radiobuttonImmediate.Visible = false;
                        radiobuttonSchedule.Visible = false;

                        txtTime.Enabled = true;
                        txtDate.Enabled = true;
                        ImgBntCalc.Enabled = true;
                        txtName.Text = dtDetail.Rows[0]["Name"].ToString();
                        dropdownDefRun.SelectedValue = dtDetail.Rows[0]["RunDefID"].ToString();
                        DateTime dtScheduled;
                        DateTime.TryParse(dtDetail.Rows[0]["Scheduled"].ToString(), out dtScheduled);
                        txtTime.Text = dtScheduled.ToString("hh:mm:ss tt");
                        txtDate.Text = dtScheduled.ToString("MM/dd/yy");
                    }

                    cmdSave.Text = "Save";

                }
                else
                {
                    cmdSave.Text = "Add";


                    lblSchedule.Visible = true;
                    radiobuttonImmediate.Visible = true;
                    radiobuttonImmediate.Checked = true;
                    radiobuttonSchedule.Visible = true;
                    radiobuttonSchedule.Checked = false;
                    txtTime.Enabled = false;
                    txtDate.Enabled = false;
                    ImgBntCalc.Enabled = false;

                    txtName.Text = "";
                    DateTime dtScheduled = DateTime.Now;
                    txtTime.Text = dtScheduled.ToString("hh:mm:ss tt");
                    txtDate.Text = dtScheduled.ToString("MM/dd/yy");
                }
            }
        }

        protected void cmdSave_Click(object sender, EventArgs e)
        {
            string sOp = Request.QueryString["Op"].ToString();
            string sUserID = Request.LogonUserIdentity.Name;
            int iID = 0;
            if (sOp == "Edit")
            {
                iID = Convert.ToInt32(Server.UrlDecode(Request.QueryString["ID"].ToString()));
            }

            if (txtName.Text.Trim().Length == 0)
            {
                lblErrorMessage.Text = "ERROR: You must enter a name.";
                return;
            }
            if (dropdownDefRun.SelectedValue == "0")
            {
                lblErrorMessage.Text = "ERROR: You must select a Run Definition.";
                return;
            }

            DateTime dtSchedule;
            if (!DateTime.TryParse(txtDate.Text + " " + txtTime.Text, out dtSchedule))
            {
                lblErrorMessage.Text = "ERROR: Date and/or time is invalid.";
                return;
            }

            using (ATBSchemaDB objATBDB = new ATBSchemaDB())
            {
                objATBDB.UpdateRuns(iID, txtName.Text, Convert.ToInt32(dropdownDefRun.SelectedValue), dtSchedule, sUserID);
            }

            Response.Redirect("RunsHome.aspx");
        }

        protected void cmdCancel_Click(object sender, EventArgs e)
        {

            Response.Redirect("RunsHome.aspx");

        }

        protected void radiobuttonImmediate_CheckedChanged(object sender, EventArgs e)
        {
            if(radiobuttonImmediate.Checked)
            {
                if(txtDate.Enabled)
                {
                    txtTime.Enabled = false;
                    txtDate.Enabled = false;
                    ImgBntCalc.Enabled = false;

                    DateTime dtScheduled = DateTime.Now;
                    txtTime.Text = dtScheduled.ToString("hh:mm:ss tt");
                    txtDate.Text = dtScheduled.ToString("MM/dd/yy");

                    radiobuttonSchedule.Checked = false;
                }
            }
        }

        protected void radiobuttonSchedule_CheckedChanged(object sender, EventArgs e)
        {
            if (radiobuttonSchedule.Checked)
            {
                if (!txtDate.Enabled)
                {
                    txtTime.Enabled = true;
                    txtDate.Enabled = true;
                    ImgBntCalc.Enabled = true;

                    radiobuttonImmediate.Checked = false;
                }
            }
        }
    }
}